package com.agger.harmonyosthemeseditor.userinterface;

import com.agger.harmonyosthemeseditor.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

public class RequestDialog
{
    private Context mContext;
    private String mText,mEmpty,mAccent;
    private boolean TextCenterMode,isRed;
    private boolean mOutsideTouchClosable,mMovable;
    private CommonDialog sDialog;
    private Component.ClickedListener mAccentButtonListener;
    private DisplayAttributes displayAttributes;
    public boolean isDestroyed = false;

    public RequestDialog(Context context)
    {
        mContext = context;
    }

    public RequestDialog setText(String text)
    {
        TextCenterMode = false;
        mText = text;
        return this;
    }

    public RequestDialog setText(String text,boolean centerMode)
    {
        TextCenterMode = centerMode;
        mText = text;
        return this;
    }

    public RequestDialog setEmptyButton(String buttonText)
    {
        mEmpty = buttonText;
        return this;
    }

    public RequestDialog setAccentButton(String buttonText,Component.ClickedListener listener)
    {
        isRed = false;
        mAccent = buttonText;
        mAccentButtonListener = listener;
        return this;
    }

    public RequestDialog setAccentButton(String buttonText,Component.ClickedListener listener,boolean redType)
    {
        isRed = redType;
        mAccent = buttonText;
        mAccentButtonListener = listener;
        return this;
    }

    public RequestDialog setOutsideTouchClosable(boolean closable)
    {
        mOutsideTouchClosable = closable;
        return this;
    }

    public RequestDialog setMovable(boolean movable)
    {
        mMovable = movable;
        return this;
    }

    public void show()
    {
        float sDim = (ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_color_mode,-1)==-16777216)?0.4f:0.2f;
        displayAttributes = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes();
        int maxHeight = displayAttributes.height - (int)((42f*displayAttributes.scalDensity)+0.5f);
        sDialog = new CommonDialog(mContext);
        int DialogWidth;
        if((((float)displayAttributes.height)/((float)displayAttributes.width))>1.48f)
        {
            DialogWidth = displayAttributes.width - (int)((24f*displayAttributes.scalDensity)+0.5f);
            sDialog.setAlignment(LayoutAlignment.BOTTOM);
            sDialog.setOffset(0,(int)((16f*displayAttributes.scalDensity)+0.5f));
        }
        else
        {
            if((((float)displayAttributes.width)/((float)displayAttributes.height))<1.48f) DialogWidth = displayAttributes.width - (int)((118f*displayAttributes.scalDensity)+0.5f);
            else DialogWidth = (int)((displayAttributes.width*0.4775f)+0.5f);
            if(DialogWidth < (int)((320f*displayAttributes.scalDensity)+0.5f))
                DialogWidth = (int)((320f*displayAttributes.scalDensity)+0.5f);
            else if(DialogWidth > (int)((400f*displayAttributes.scalDensity)+0.5f))
                DialogWidth = (int)((400f*displayAttributes.scalDensity)+0.5f);
            sDialog.setAlignment(LayoutAlignment.CENTER);
            sDialog.setOffset(0,0);
        }
        sDialog.setTransparent(true);
        Component dialogLayout = initDialog();
        sDialog.setContentCustomComponent(dialogLayout);
        sDialog.setSize(DialogWidth, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        sDialog.setAutoClosable(mOutsideTouchClosable);
        sDialog.setMovable(mMovable);
        sDialog.show();
        Window sWindow = sDialog.getWindow();
        WindowManager.LayoutConfig layoutConfig =sWindow.getLayoutConfig().get();
        layoutConfig.dim = sDim;
        sWindow.setLayoutConfig(layoutConfig);
        int dialogContentHeight = dialogLayout.getHeight();
        if(dialogContentHeight>maxHeight)
        {
            Component contentScroller = dialogLayout.findComponentById(ResourceTable.Id_content_scroller);
            int scrollerHeight = contentScroller.getHeight()-dialogContentHeight+maxHeight;
            CommonDialog oldDialog = sDialog;
            sDialog = new CommonDialog(mContext);
            if((((float)displayAttributes.height)/((float)displayAttributes.width))>1.48f)
            {
                sDialog.setAlignment(LayoutAlignment.BOTTOM);
                sDialog.setOffset(0,(int)((16f*displayAttributes.scalDensity)+0.5f));
            }
            else
            {
                sDialog.setAlignment(LayoutAlignment.CENTER);
                sDialog.setOffset(0,0);
            }
            sDialog.setTransparent(true);
            dialogLayout = initDialog();
            contentScroller = dialogLayout.findComponentById(ResourceTable.Id_content_scroller);
            contentScroller.setHeight(scrollerHeight);
            ((ScrollView)contentScroller).setReboundEffect(true);
            dialogLayout.setHeight(maxHeight);
            sDialog.setContentCustomComponent(dialogLayout);
            sDialog.setSize(DialogWidth, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            sDialog.setAutoClosable(mOutsideTouchClosable);
            sDialog.setMovable(false);
            sDialog.show();
            sWindow = sDialog.getWindow();
            layoutConfig =sWindow.getLayoutConfig().get();
            layoutConfig.dim = sDim;
            sWindow.setLayoutConfig(layoutConfig);
            oldDialog.hide();
            oldDialog.destroy();
        }
        isDestroyed = false;
        System.gc();
    }

    private Component initDialog()
    {
        Component dialogLayout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_request_dialog, null, false);
        Component requestDialogLayout = dialogLayout.findComponentById(ResourceTable.Id_RequestDialogLayout);
        requestDialogLayout.setBackground(new ShapeElement(){{setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_radio_dialog_bg,-1)));setCornerRadius((int)((24f*displayAttributes.scalDensity)+0.5f));}});
        Text text = (Text)dialogLayout.findComponentById(ResourceTable.Id_text);
        if(TextCenterMode) text.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        text.setText(mText);
        Button emptyButton = (Button)dialogLayout.findComponentById(ResourceTable.Id_EmptyButton);
        emptyButton.setBackground(new GeneralButtonElement(mContext,GeneralButtonElement.TYPE_Empty_Button));
        emptyButton.setTextColor(new Color(ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_button_accent,-417762)));
        emptyButton.setText(mEmpty);
        emptyButton.setClickedListener(component -> { if(sDialog != null) {sDialog.destroy(); isDestroyed=true; System.gc();} });
        Button accentButton = (Button)dialogLayout.findComponentById(ResourceTable.Id_AccentButton);
        accentButton.setBackground(new GeneralButtonElement(mContext,GeneralButtonElement.TYPE_Empty_Button));
        accentButton.setTextColor(new Color(ResourceTool.getColor(mContext,isRed?ResourceTable.Color_dawnlighti_accent_request_red:ResourceTable.Color_dawnlighti_button_accent,-417762)));
        accentButton.setText(mAccent);
        accentButton.setClickedListener(mAccentButtonListener);
        return dialogLayout;
    }


    public boolean isShowing()
    {
        return sDialog.isShowing();
    }

    public void destory()
    {
        sDialog.destroy();
        isDestroyed = true;
    }
}